
/*
Copyright (c) 2014, ebaklund
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution.

3. Neither the name of the <ORGANIZATION> nor the names of its contributors 
   may be used to endorse or promote products derived from this software 
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/


#include "stdafx.h"
#include "CppUnitTest.h"

#include <functional>
#include <iostream>
#include <vector>


// http://codereview.stackexchange.com/questions/36251/c-delegate-implementation-with-member-functions
// http://coliru.stacked-crooked.com/a/55858bdee7561e79


using namespace Microsoft::VisualStudio::CppUnitTestFramework;


namespace UnitTests
{
  template <typename... Params>
  class Delegate
  {
  private:
    typedef std::function<void(Params...)> Handler;
    typedef std::vector<Handler> Vector;
    Vector m_handlers;

  public:
    typedef typename Vector::const_iterator cookie;

    cookie connect(Handler&& func)
    {
      m_handlers.push_back(std::move(func)); // Technically correct for context, but generally bad pattern
//      m_handlers.push_back(std::forward(func)); // Should be generally be correct but does not work. Why?
      return m_handlers.begin() + m_handlers.size() - 1; // This is bad as iterators easily becomes obsolete
    }

    template <typename... BindArgs, typename Sfinae = typename std::enable_if<(sizeof...(BindArgs)>1), void>::type>
    cookie connect(BindArgs&&... args)
    {
      return connect(Handler(std::bind(std::forward<BindArgs>(args)...)));
    }

    void disconnect(cookie which)
    {
      m_handlers.erase(which);
    }

    template<typename... Args> void operator()(Args... args)
    {
      for(auto const& handler : m_handlers)
        handler(args...);
    }
  };

  ///

#define CV volatile const

  struct BaseClass
  {
    virtual void print(const std::string& str) CV = 0;
  };


  struct A : BaseClass
  {
    A() {}

    virtual void print(const std::string& str) CV 
    {
      std::cout << "Class A : Print (" << str << ")\n";
    }
  };


  struct B : BaseClass
  {
    B() {}

    virtual void print(const std::string& str) CV 
    {
      std::cout << "Class B : Print (" << str << ")\n";
    }
  };
  
  
  ///

  using namespace std::placeholders;


	TEST_CLASS(Articles_SeheDelegateTests)
	{
	public:
		
		TEST_METHOD(TestSeheDelegateTests)
		{
			Assert::IsTrue(true);
		}
		
		TEST_METHOD(TestSeheDelegates)
		{
			Delegate <const std::string&> delegate;
      A CV a;
      B CV b;
#if 0 // Does not compile on VS2013
      auto acookie = delegate.connect(&A::print, std::ref(a), _1); // Compile time error
      auto bcookie = delegate.connect(&B::print, std::ref(b), _1); // Compile time error

      auto acookie = delegate.connect(&A::print, &a, _1); // Runtime error
      auto bcookie = delegate.connect(&B::print, &b, _1); // Runtime error

      delegate("hello");
      delegate("world");

      delegate.disconnect(acookie);
      delegate("bye world");

      delegate.disconnect(bcookie);
      delegate("nobody there.");  // should not print anything
#endif // 0
    }
	};
}